<?php
            
use app\model\Theme; 



class Prize_Page extends PluginWebPage {
    function main(){
         global $_G,$_P,$_S,$_C;
         $system = $_S;
         $config = $_C;
         $method = 'gift_prize';
         $themeid = intval($_P['themeid']);
         if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $theme = Theme::getThemeById($themeid);

        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if($system['is_multiple_merchants']&& $config['showshop']==0){
            $shangjia_list = GiftModel::getShangjiaList();
        }else{
            $shangjia_list = array();
        }

        $gift_record =  GiftModel::getGiftList($themeid,$search_data);
        $is_new_wxapp_version = $system['is_new_wxapp'];
        if($shangjia_list){
            foreach ($gift_record as $key => $gift){
                if($gift['shangjiaid']){
                    $gift_record[$key]['shangjia_name'] = $shangjia_list[$gift['shangjiaid']]['name'];
                }else{
                    $gift_record[$key]['shangjia_name'] ='';
                }
            }
        }
         $return_clicent = get_defined_vars();
return $this->view('/gift/prize/main', $return_clicent);
    }

    function editGift(){
        global $_G,$_P,$_S,$_C;
        $id = $_P['id'];
        $themeid = intval($_P['themeid']);
        $info = GiftModel::getGiftItemById($themeid,$id);
        show_json(1,['msg'=>'查询成功','data'=>$info]);
    }

    function deleteGift(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        $classify = $_P['classify'];
        $taskid = $_P['taskid'];
        $check =  GiftModel::deleteCheckGift($themeid,$classify,$taskid);
        if(!$check){
            show_json(0,['msg'=>'存在关联记录，删除失败，请下架']);
        }else{
            show_json(1,['msg'=>'删除成功']);
        }
    }

    function checkConfigGift(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        $gift_id = $_P['gift_id'];
        $check =  GiftModel::checkConfigGift($themeid,$gift_id);
        if($check['status']==0){
            show_json(0,$check['msg']);
        }else{
            show_json(1,['msg'=>'无配置']);
        }
    }

    function delGift(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        $gift_id = $_P['gift_id'];
        $where['id'] =  $gift_id;
        $where['themeid'] =  $themeid;
        $data['status'] = 0;
        GiftModel::editGiftItem($where,$data);
        yxlog($data,'邀请礼删除奖品');
        show_json(1,['msg'=>'删除成功']);
    }

    function postGift()
    {
        global $_G, $_P,$_S,$_C;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);

        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,['msg'=>'活动入口不正确']);
            return;
        }

        $name = trim($_P['name']);
        $top_img = tomedia($_P['top_img']);
        $price = trim($_P['price']);
        $type = $_P['type']?intval($_P['type']):1;
        $status = $_P['status']?intval($_P['status']):1;;
        $shangjiaid = isset($_P['shangjiaid'])?intval($_P['shangjiaid']):0;
        $remark = isset($_P['remark'])?$_P['remark']:"";
        if (!$name){
            show_json(0,['msg'=>'礼品名称不能为空']);
        }
        if (!$top_img){
            show_json(0,['msg'=>'礼品头图未设置']);
        }
//        if (!$price){
//            show_json(0,['msg'=>'礼品市场价格未设置']);
//        }

        $data = [
            'shangjiaid'=>$shangjiaid,
            'name'=>$name,
            'top_img'=>$top_img,
            'price'=>$price,
            'status'=>$status,
            'type'=>$type,
            'remark'=>$remark,
        ];
        if (!$id){
            $data['software'] = MODULE_NAME;
            $data['themeid'] = $themeid;
            $data['created_at'] = date("Y-m-d H:i:s",time());
            yxlog($data,'邀请礼奖品添加');
            $res = GiftModel::insertGift($data);
        }else{
            $old_item = GiftModel::getGiftItemById($themeid,$id);
            unset($old_item['shangjia_name']);
            $type = '邀请礼奖品编辑';
            writelog($themeid,$type,$old_item,$data,$_G['username']);
            $where['id'] = $id;
            $where['software'] = MODULE_NAME;
            $where['themeid'] = $themeid;
            $res = GiftModel::editGiftItem($where,$data);
        }
        GiftModel::getGiftNameLength($themeid);
        if (!$res){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }



}
